home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 4982 < prev    next >
Encoding:
Text File  |  1996-08-06  |  1.7 KB  |  65 lines

  1. Path: ns1.win.net!dorsea!nehager
  2. Newsgroups: comp.lang.c++
  3. Message-ID: <1476@dorsea.win.net>
  4. Reply-To: nehager@dorsea.win.net (Nat Hager III)
  5. From: nehager@dorsea.win.net (Nat Hager III)
  6. Date: Thu, 01 Feb 1996 22:23:00 GMT
  7. Subject: disk I/O - large binary array problem
  8.  
  9. Hi folks,
  10.  
  11. I'm having a problem writing large arrays to disk using the
  12. ofstream and ifstream write and read functions.  Things work
  13. properly for the first 100 or so elements, but then the rogue
  14. value appears (like 3.9E-320), and the whole thing bombs out, with
  15. the remaining elements written as zero.  An ideas? Could it be a
  16. stream flushing problem, a buffer problem, a stack problem, crossing
  17. a segment boundary? Sorry if these are dumb suggestions, but I'm a
  18. relative newbie. 
  19.  
  20. I've tried writing the data both as a struct and on an
  21. element-by-element basis as an array, so I don't think it has to
  22. do with my struct usage. Here's a sample program: 
  23.  
  24. Thank you,
  25. Nat Hager 
  26.  
  27. ---------snip--------snip--------snip---------
  28.  
  29.  
  30. struct datafile
  31.     {
  32.     double test_array1[400];
  33.     };
  34.  
  35. datafile tdr;
  36.  
  37. void file_write()
  38.    {
  39.    for (int i=0;i<400;i++)
  40.        { 
  41.        tdr.test_array1[i] = i/1e9;  // initialize it to something
  42.        }
  43.    ofstream tfile("crap.dat");             // binary write to file
  44.    tfile.write( (char*) &tdr, sizeof tdr );
  45.    tfile.close;
  46.    }
  47.  
  48. void file_read() 
  49.    { 
  50.    ifstream tfile("crap.dat");
  51.    tfile.read((char*) &tdr, sizeof tdr ); // binary read from file
  52.    for ( int i=0; i<400;i++) 
  53.         {
  54.         cout << i << '\t' << tdr.test_array1 [i] << '\n'; //print 
  55.         }
  56.    tfile.close;
  57.    }
  58.  
  59.  
  60. Nat Hager III             nehager@dorsea.win.net
  61. 772 Dorsea Rd.          71552.513@compuserve.com
  62. Lancaster, PA 17601      hagerne@vax.etown.edu 
  63. (717) 898-3053              (717) 361-1377        
  64.  
  65.